<?php
	require_once $ubi['SERVER']['SERVER_ROOT'] . '/utility/generalFunctions.php';

	require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busResource.php';
	
	require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/daoAvatar.php';
	
	class AvatarBus {
		private $_avatarDao;	
		
		public function __construct() {
			$this->_avatarDao = new AvatarDao();		
		}
		
		public function addAvatar($avatar) {
			return $this->_avatarDao->addAvatar($avatar);
		}		
		
		public function getResource($userID) {
			return $this->_avatarDao->getResource($userID);
		}
		
		public function getAvatarPathOfUsers($users) {
			global $ubi;
			
			$imgs = array();
			
			foreach ($users as $user) {
				$resource = $this->_avatarDao->getResource($user->getUserID());
				if ($resource) {
					GeneralFunctions::resizeImageResource($resource->getResourceName());
					$imgs[$user->getUserID()] = $resource->getResourceName(); 
				} else {
					$imgs[$user->getUserID()] = $ubi['USER']['DEFAULT_AVATAR_RESOURCE'];
				}
			}
			return $imgs;
		}
		
		public function getResources($userID) {
			$arrResourceID = $this->_avatarDao->getResources($userID);
			$resources = array();
			$resourceBus = new ResourceBus();
			foreach ($arrResourceID as $resID) {
				$resource = $resourceBus->getResource($resID);
				$resources[] = $resource;
			}
			
			return $resources;
		}
		
		public function getAvatars($userID) {
			return $this->_avatarDao->getAvatars($userID);
		}
		
		public function setDefaultAvatar($userID, $resourceID) {
			return $this->_avatarDao->setDefaultAvatar($userID, $resourceID);
		}
		
		public function deleteAvatar($resourceID) {
			global $ubi;
			
			$resourceBus = new ResourceBus();
			$img = $resourceBus->getImgOfResource($resourceID);
			$path = $ubi['SERVER']['SERVER_ROOT'] . '\\' . $ubi['SERVER']['RESOURCE_PATH'];
				if (unlink($path . '\\' . $img)) {
					return $this->_avatarDao->deleteAvatar($resourceID);
				}
		}
	} 
?>